<?php
$num = DB::affected(DB::query("SELECT Id FROM app_key"));
?>
<div class="container-fluid">

<div class="panel panel-primary">
  <div class="panel-heading">
    用户列表-共有<?=$num?>条卡密
  </div>
  <div class="panel-body">
  <div class="table-responsive">
  <table class="table table-borderless">
   <thead>
    <tr>
      <th>卡密</th>
      <th>金额</th>
      <th>状态</th>
      <th>操作</th>
      <th>时间</th>
    </tr>
  </thead>
   <tbody>
<?php
//判断是否有分页参数
if (!isset($_GET['page'])) {
  header('Location: ?s=keylist&page=1');
}
//获取数据
$p = ($_GET['page']-1)*8;
$result = DB::query("SELECT * FROM app_key ORDER BY Id DESC limit $p, 8");
  if (DB::affected($result) > 0) {
    // 输出数据
    while($row = DB::fetch($result)) {
    if($row["user"] != NULL){
        $vip = '<i class="icon icon-smile" style="color:#38b03f"></i>';
    }else{
        $vip = '<i class="icon icon-frown" style="color:#f00"></i>';
    }
      echo '<tr><td>'.htmlspecialchars($row['name']).'</td><td>'.htmlspecialchars($row['money']).'</td><td>'.$vip.'</td><td><button class="btn btn-xs btn-danger" id="del" bs="'.$row['Id'].'">删除</button>&nbsp;<a href="./?s=setuser&id='.$row['Id'].'" class="btn btn-xs btn-success">详细</a></td><td>'.$row['time'].'</td></tr>';
    }
  }
$page = $_GET['page'];
?>
</tbody>
</table>
<ul class="pager">
<li class="previous"><a href="./?s=keylist&page=1">«</a></li>
<?php
//四舍五入，循环输出页
$num = round($num/8);
for ($i=1; $i < $num+1; $i++) {
  if ($page == $i) {
    echo '<li class="active"><a href="./?s=keylist&page='.$i.'">'.$i.'</a></li>';;
  }else{
    echo '<li><a href="./?s=keylist&page='.$i.'">'.$i.'</a></li>';
  }
}
?>
<li class="next"><a href="./?s=keylist&page=<?=$num?>">»</a></li>
</ul>
</div>
</div>
</div>
<script>
$("#del").on("click", function () {
                var id = $(this).attr("bs");
                $.post("../api.php", {
                    index:'delekey',
                    id:id
                }, function (msg) {
                   var json = JSON.parse(msg);
                    if (0 == json['code']) {
                        new $.zui.Messager(json['msg'], {
                          type: 'danger' // 定义颜色主题
                        }).show();
                    }else{
                        new $.zui.Messager(json['msg'], {
                          type: 'success' // 定义颜色主题
                        }).show();
                        setTimeout(function() {
                          window.location.href="?s=keylist&page=1";
                        }, 2000);                     
                    }
                })
            })

</script>
</div>
</body>
</html>